package com.hzncc.flowable_diboot.service.refundmanage.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.binding.QueryBuilder;
import com.diboot.core.util.V;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.entity.BaseLoginUser;
import com.diboot.iam.util.IamSecurityUtils;
import com.diboot.notification.config.Cons;
import com.diboot.notification.entity.Message;
import com.hzncc.flowable_diboot.dto.refundmanage.RefundRegisterDTO;
import com.hzncc.flowable_diboot.entity.Customer;
import com.hzncc.flowable_diboot.entity.SapCustomer;
import com.hzncc.flowable_diboot.entity.refundmanage.RefundContract;
import com.hzncc.flowable_diboot.rabbitmq.service.impl.SendMessageService;
import com.hzncc.flowable_diboot.service.CustomerService;
import com.hzncc.flowable_diboot.service.SapCustomerService;
import com.hzncc.flowable_diboot.util.ConstantUtil;
import com.hzncc.flowable_diboot.util.MessageCons;
import com.hzncc.flowable_diboot.vo.refundmanage.RefundContractListVO;
import com.hzncc.flowable_diboot.vo.refundmanage.RefundRegisterListVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.diboot.core.util.S;
import com.diboot.core.service.impl.BaseServiceImpl;

import com.hzncc.flowable_diboot.entity.refundmanage.RefundRegister;

import com.hzncc.flowable_diboot.mapper.refundmanage.RefundRegisterMapper;
import com.hzncc.flowable_diboot.service.refundmanage.RefundRegisterService;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
* 退款登记 相关Service实现类
* @author MyName
* @version 1.0
* @date 2024-11-08
* Copyright © MyCorp
*/
@Slf4j
@Service
public class RefundRegisterServiceImpl extends BaseServiceImpl<RefundRegisterMapper, RefundRegister> implements RefundRegisterService {

    @Resource
    private SendMessageService sendMessageService;

    @Resource
    private CustomerService customerService;

    @Resource
    private SapCustomerService sapCustomerService;

    @Override
    public List<RefundRegisterListVO> getRefundRegisterList(RefundRegisterDTO queryDto, Pagination pagination) {
        if (StringUtils.isNotEmpty(queryDto.getCustomerName())){
            LambdaQueryWrapper<SapCustomer> customerQuery = new LambdaQueryWrapper<>();
            customerQuery.like(SapCustomer::getName, "%" + queryDto.getCustomerName()+ "%");
            List<SapCustomer> entityList = sapCustomerService.getEntityList(customerQuery);
            List<String> collect = entityList.stream().map(SapCustomer::getId).collect(Collectors.toList());
            queryDto.setCustomerIds(collect);
            queryDto.setCustomerName(null);
        }
        QueryWrapper<RefundRegister> queryWrapper = QueryBuilder.toQueryWrapper(queryDto);
        if (pagination != null && V.isEmpty(pagination.getOrderBy())) {
            pagination.setOrderBy(Pagination.ORDER_BY_ID_DESC);
        }

        return this.getViewObjectList(queryWrapper, pagination, RefundRegisterListVO.class);
    }

    @Override
    public boolean createRefundRegister(RefundRegisterListVO refundRegister) {
        BaseLoginUser currentUser = IamSecurityUtils.getCurrentUser();
        refundRegister.setCerted("0");
        boolean success = this.createEntity(refundRegister);
        String copyRead = refundRegister.getCopyRead();
        if (StringUtils.isNotEmpty(copyRead)){
            String[] split = copyRead.split(",");
            List<String> copyReadList = Arrays.asList(split);
            for (String uid : copyReadList){
                // 发送传阅消息
                sendMessageService.sendRetainedMessage(new Message().setChannel(Cons.MESSAGE_CHANNEL.SYS_MSG.name())
                        .setTitle(MessageCons.REFUND_REGISTER_TITLE)
                        .setReceiver(uid).setSender(currentUser.getId())
                        .setContent(String.format(ConstantUtil.REFUND_REGISTER_COPY_READ, currentUser.getDisplayName(), refundRegister.getCustomerName()))
                        .setStatus(Cons.MESSAGE_STATUS.DELIVERY.name()).setAppModule(ConstantUtil.MODEL_REFUND).setTemplateId("")
                        .setBusinessCode(MessageCons.REFUND_REGISTER_CODE).setBusinessType(MessageCons.REFUND_REGISTER_TYPE)
                        .setChannel(Cons.MESSAGE_CHANNEL.SYS_MSG.name()));
            }
        }
        String salesCopyRead = refundRegister.getSalerCopyRead();
        if (StringUtils.isNotEmpty(salesCopyRead)){
            String[] split = salesCopyRead.split(",");
            List<String> salesCopyReadList = Arrays.asList(split);
            for (String uid : salesCopyReadList){
                // 发送传阅消息
                sendMessageService.sendRetainedMessage(new Message().setChannel(Cons.MESSAGE_CHANNEL.SYS_MSG.name())
                        .setTitle(MessageCons.REFUND_REGISTER_TITLE)
                        .setReceiver(uid).setSender(currentUser.getId())
                        .setContent(String.format(ConstantUtil.REFUND_REGISTER_COPY_READ, currentUser.getDisplayName(), refundRegister.getCustomerName()))
                        .setStatus(Cons.MESSAGE_STATUS.DELIVERY.name()).setAppModule(ConstantUtil.MODEL_REFUND).setTemplateId("")
                        .setBusinessCode(MessageCons.REFUND_REGISTER_CODE).setBusinessType(MessageCons.REFUND_REGISTER_TYPE)
                        .setChannel(Cons.MESSAGE_CHANNEL.SYS_MSG.name()));
            }
        }
        return success;
    }
}